home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Networking / GetPPPStatus / sources / MoreFinderEvents.c < prev    next >
Encoding:
Text File  |  1998-02-06  |  14.7 KB  |  583 lines  |  [TEXT/CWIE]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Functions to help you build and sending Apple events to the Finder.
  5. **
  6. **    by Andy Bachorski, Apple Developer Technical Support
  7. **
  8. **    File:        MoreFinderEvents.c
  9. **
  10. **    Version:    0.3.5
  11. **
  12. **    Copyright © 1996 Apple Computer, Inc.
  13. **    All rights reserved.
  14. **
  15. **    You may incorporate this sample code into your applications without
  16. **    restriction, though the sample code has been provided "AS IS" and the
  17. **    responsibility for its operation is 100% yours.  However, what you are
  18. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  19. **    after having made changes. If you're going to re-distribute the source,
  20. **    we require that you make it clear in the source that the code was
  21. **    descended from Apple Sample Code, but that you've made changes.
  22. */
  23.  
  24.  
  25. //    Constant used to #undef PASCAL when not compiling a library
  26. #define    COMPILING_MORE_FINDER_EVENTS    true
  27.  
  28. //    Prefix file
  29. #include "Prefix.h"
  30.  
  31.  
  32. //    System includes
  33. #include <Aliases.h>
  34. #include <AppleScript.h>
  35. #include <Components.h>
  36. #include <Folders.h>
  37. #include <Icons.h>
  38. #include <OSA.h>
  39.  
  40.  
  41. //    C standard library includes
  42. #include <string.h>
  43.  
  44.  
  45. //    MoreFinderEvents includes
  46. #include "FinderRegistry.h"
  47. #include "AEHelpers.h"
  48.  
  49.  
  50. //    Export symbols in this header for shared libraries
  51. #pragma export on
  52. #include "MoreFinderEvents.h"
  53. #pragma export off
  54.  
  55. #define    COMPILING_MORE_FINDER_EVENTS    true
  56.  
  57.  
  58. const    Boolean        flagNotSet = -1;
  59.  
  60. static    Boolean        hasAppleEvents = flagNotSet;
  61. static    Boolean        finderCallsAEProcess = flagNotSet;
  62. static    Boolean        finderIsOSLCompliant = flagNotSet;
  63.  
  64.  
  65. // *****************************************************************************
  66.  
  67. PASCAL OSErr MFESetSelectionToNone( const AEIdleUPP idleProcUPP )
  68. {
  69.     OSErr        anErr = noErr;
  70.     
  71.     AppleEvent    theEvent = { typeNull, nil };
  72.     
  73.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  74.                                               kAECoreSuite, kAESetData, &theEvent );
  75.     if ( anErr == noErr )
  76.     {
  77.         AEDesc         propertyObject = { typeNull, nil };
  78.         AEDesc        containerObj = { typeNull, nil };    // start with the null (application) container
  79.             
  80.         anErr = AEHMakePropertyObject( pSelection, &containerObj, &propertyObject );
  81.         if ( anErr == noErr )
  82.         {
  83.             anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
  84.             AEDisposeDesc( &propertyObject );
  85.             if ( anErr == noErr )
  86.             {
  87.                 AEDescList        emptyList = { typeNull, nil };
  88.                 
  89.                 anErr = AECreateList( nil, 0, false, &emptyList );
  90.                 if ( anErr == noErr )
  91.                 {
  92.                     anErr = AEPutParamDesc( &theEvent, keyAEData, &emptyList );
  93.                     if ( anErr == noErr )
  94.                     {
  95.                         AppleEvent    theReply = { typeNull, nil };
  96.                         AESendMode    sendMode;
  97.                         
  98.                         if ( idleProcUPP == nil )
  99.                             sendMode = kAENoReply;
  100.                         else
  101.                             sendMode = kAEWaitReply;
  102.  
  103.                         anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  104.                                         kNoTimeOut, idleProcUPP, nil );
  105.                         
  106.                         AEDisposeDesc( &theEvent );
  107.                         
  108.                         if ( anErr == noErr && idleProcUPP != nil )
  109.                         {
  110.                             anErr =  AEHGetHandlerError( &theReply );
  111.                             
  112.                             AEDisposeDesc( &theReply );
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.     }
  119.     
  120.     return anErr;
  121.     
  122. } // MFESetSelectionToNone
  123.  
  124. // *****************************************************************************
  125.  
  126. PASCAL OSErr MFEChangeFolderViewNewSuite( const FSSpecPtr fssPtr,
  127.                                           const long viewStyle,
  128.                                           const AEIdleUPP idleProcUPP )
  129. {
  130.     OSErr        anErr = noErr;
  131.     
  132.     AppleEvent    theEvent = {typeNull, nil};
  133.     
  134.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  135.                                               kAECoreSuite, kAESetData, &theEvent );
  136.     if ( anErr == noErr )
  137.     {
  138.         AEDesc         folderObject = {typeNull, nil};
  139.         AEDesc        containerObj = { typeNull, nil };    // start with the null (application) container
  140.         
  141.         anErr = AEHMakeAliasObjectFromFSSpec( fssPtr, &containerObj, &folderObject );
  142.         if ( anErr == noErr )
  143.         {
  144.             AEDesc         propertyObject = {typeNull, nil};
  145.             
  146.             anErr = AEHMakePropertyObject( pView, &folderObject, &propertyObject );    // disposes of folderObject for us
  147.             if ( anErr == noErr )
  148.             {
  149.                 anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
  150.                 AEDisposeDesc( &propertyObject );
  151.                 if ( anErr == noErr )
  152.                 {
  153.                     anErr = AEPutParamPtr( &theEvent, keyAEData,
  154.                                            typeLongInteger, &viewStyle, sizeof(viewStyle) );
  155.                     if ( anErr == noErr )
  156.                     {
  157.                         AppleEvent    theReply = {typeNull, nil};
  158.                         AESendMode    sendMode;
  159.                         
  160.                         if ( idleProcUPP == nil )
  161.                             sendMode = kAENoReply;
  162.                         else
  163.                             sendMode = kAEWaitReply;
  164.  
  165.                         anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  166.                                         kNoTimeOut, idleProcUPP, nil );
  167.                         
  168.                         AEDisposeDesc( &theEvent );
  169.                         
  170.                         if ( anErr == noErr && idleProcUPP != nil )
  171.                         {
  172.                             anErr =  AEHGetHandlerError( &theReply );
  173.                             
  174.                             AEDisposeDesc( &theReply );
  175.                         }
  176.                     }
  177.                 }
  178.             }
  179.         }
  180.     }
  181.     
  182.     return anErr;
  183.     
  184. } // MFEChangeFolderView
  185.  
  186. // *****************************************************************************
  187.  
  188. PASCAL OSErr MFEChangeFolderViewOldSuite( const FSSpecPtr fssPtr,
  189.                                           const long viewStyle,
  190.                                           const AEIdleUPP idleProcUPP )
  191. {
  192.     OSErr        anErr = noErr;
  193.     
  194.     AppleEvent    theEvent = {typeNull, nil};
  195.     
  196.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  197.                                               kAEFinderEvents, kAEChangeView, &theEvent );
  198.     if ( anErr == noErr )
  199.     {
  200.         AEDesc    aliasObject = { typeNull, nil };    // start with the null (application) container
  201.         
  202.         anErr = AEHMakeAliasDescFromFSSpec( fssPtr, &aliasObject );
  203.         
  204.         if ( anErr == noErr )
  205.         {
  206.             anErr = AEPutParamDesc( &theEvent, keyDirectObject, &aliasObject );
  207.             AEDisposeDesc( &aliasObject );
  208.             
  209.             if ( anErr == noErr )
  210.             {
  211.                 anErr = AEPutParamPtr( &theEvent, keyMiscellaneous,
  212.                                        typeLongInteger, &viewStyle, sizeof( viewStyle ) );
  213.                 if ( anErr == noErr )
  214.                 {
  215.                     AppleEvent    theReply = {typeNull, nil};
  216.                     AESendMode    sendMode;
  217.                     
  218.                     if ( idleProcUPP == nil )
  219.                         sendMode = kAENoReply;
  220.                     else
  221.                         sendMode = kAEWaitReply;
  222.  
  223.                     anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  224.                                     kNoTimeOut, idleProcUPP, nil );
  225.                     
  226.                     AEDisposeDesc( &theEvent );
  227.                     
  228.                     if ( anErr == noErr && idleProcUPP != nil )
  229.                     {
  230.                         anErr =  AEHGetHandlerError( &theReply );
  231.                         
  232.                         AEDisposeDesc( &theReply );
  233.                     }
  234.                 }
  235.             }
  236.         }
  237.     }
  238.     
  239.     return anErr;
  240.     
  241. } // MFEChangeFolderViewOldSuite
  242.  
  243. // *****************************************************************************
  244.  
  245. PASCAL OSErr MFEChangeFolderView( const FSSpecPtr fssPtr,
  246.                                   const long viewStyle,
  247.                                   const AEIdleUPP idleProcUPP )
  248. {
  249.     OSErr    anErr = noErr;
  250.     
  251.     if ( FinderCallsAEProcess() )
  252.     {
  253.         if ( FinderIsOSLCompliant() )
  254.         {
  255.             anErr = MFEChangeFolderViewNewSuite( fssPtr, viewStyle, idleProcUPP );
  256.         }
  257.         else
  258.         {
  259.             anErr = MFEChangeFolderViewOldSuite( fssPtr, viewStyle, idleProcUPP );
  260.         }
  261.     }
  262.     else
  263.     {
  264.         anErr = errAEEventNotHandled;
  265.     }
  266.     
  267.     return anErr;
  268. }//end MFEChangeFolderView
  269.  
  270. // *****************************************************************************
  271.  
  272. PASCAL OSErr MFEAddCustomIconToItem( const FSSpecPtr fssPtr,
  273.                                      const Handle theIconSuite,
  274.                                      const IconSelectorValue iconSelector,
  275.                                      const AEIdleUPP idleProcUPP )
  276. {
  277.     OSErr        anErr = noErr;
  278.     
  279.     AppleEvent    theEvent = {typeNull, nil};
  280.     
  281.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  282.                                               kAECoreSuite, kAESetData, &theEvent );
  283.     if ( anErr == noErr )
  284.     {
  285.         AEDesc         itemObject = {typeNull, nil};
  286.         AEDesc        containerObj = { typeNull, nil };        // start with the null (application) container
  287.         
  288.         anErr = AEHMakeAliasObjectFromFSSpec( fssPtr, &containerObj, &itemObject );
  289.         
  290.         if ( anErr == noErr )
  291.         {
  292.             AEDesc         propertyObject = {typeNull, nil};
  293.             
  294.             anErr = AEHMakePropertyObject( pIconBitmap, &itemObject, &propertyObject );    // disposes of itemObject for us
  295.             
  296.             if ( anErr == noErr )
  297.             {
  298.                 anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
  299.                 AEDisposeDesc( &propertyObject );
  300.                 
  301.                 if ( anErr == noErr )
  302.                 {
  303.                     AEDescList    iconFamilyRec = { typeNull, nil };
  304.                     
  305.                     anErr = AEHMakeIconFamilyRecord( theIconSuite, iconSelector, &iconFamilyRec );
  306.  
  307.                     if ( anErr == noErr )
  308.                     {
  309.                         anErr = AEPutParamDesc( &theEvent, keyAEData, &iconFamilyRec );
  310.                         
  311.                         if ( anErr == noErr )
  312.                         {
  313.                             AppleEvent    theReply = {typeNull, nil};
  314.                             AESendMode    sendMode;
  315.                             
  316.                             if ( idleProcUPP == nil )
  317.                                 sendMode = kAENoReply;
  318.                             else
  319.                                 sendMode = kAEWaitReply;
  320.  
  321.                             anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  322.                                             kNoTimeOut, idleProcUPP, nil );
  323.                             
  324.                             AEDisposeDesc( &theEvent );
  325.                             
  326.                             if ( anErr == noErr )
  327.                             {
  328.                                 anErr =  AEHGetHandlerError( &theReply );
  329.                                 
  330.                                 AEDisposeDesc( &theReply );
  331.                             }
  332.                         }
  333.                     }
  334.                 }
  335.             }
  336.         }
  337.     }
  338.     
  339.     return anErr;
  340.     
  341. } // MFEAddCustomIconToItem
  342.  
  343. // *****************************************************************************
  344.  
  345. PASCAL OSErr MFEGetItemIcon( const FSSpecPtr fssPtr,
  346.                              const AEIdleUPP idleProcUPP,
  347.                                    Handle     *theIconSuite )
  348. {
  349.     OSErr    anErr = noErr;
  350.     
  351.     AppleEvent    theEvent = {typeNull, nil};
  352.     
  353.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  354.                                               kAECoreSuite, kAEGetData, &theEvent );
  355.     if ( anErr == noErr )
  356.     {
  357.         AEDesc         itemObject = {typeNull, nil};
  358.         AEDesc        containerObj = { typeNull, nil };        // start with the null (application) container
  359.         
  360.         anErr = AEHMakeAliasObjectFromFSSpec( fssPtr, &containerObj, &itemObject );
  361.         
  362.         if ( anErr == noErr )
  363.         {
  364.             AEDesc         propertyObject = {typeNull, nil};
  365.             
  366.             anErr = AEHMakePropertyObject( pIconBitmap, &itemObject, &propertyObject );    // disposes of itemObject for us
  367.             
  368.             if ( anErr == noErr )
  369.             {
  370.                 anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
  371.                 AEDisposeDesc( &propertyObject );
  372.                 
  373.                 if ( anErr == noErr )
  374.                 {
  375.                     AppleEvent    theReply = {typeNull, nil};
  376.                     AESendMode    sendMode;
  377.                     
  378.                     if ( idleProcUPP == nil )
  379.                         sendMode = kAENoReply;
  380.                     else
  381.                         sendMode = kAEWaitReply;
  382.  
  383.                     anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  384.                                     kNoTimeOut, idleProcUPP, nil );
  385.                     
  386.                     AEDisposeDesc( &theEvent );
  387.                     
  388.                     if ( anErr == noErr )
  389.                     {
  390.                         anErr =  AEHGetHandlerError( &theReply );
  391.                         
  392.                         if ( anErr == noErr )
  393.                         {
  394.                             AEDesc    iconFamilyRec = { typeNull, nil };
  395.                             
  396.                             anErr = AEGetParamDesc( &theReply, keyDirectObject, typeWildCard, &iconFamilyRec );
  397.                             AEDisposeDesc( &theReply );
  398.                             
  399.                             if ( anErr == noErr )
  400.                             {
  401.                                 anErr = AEHMakeIconSuite( &iconFamilyRec, theIconSuite );
  402.                             }
  403.                             AEDisposeDesc( &iconFamilyRec );
  404.                         }
  405.                     }
  406.                 }
  407.             }
  408.         }
  409.     }
  410.     
  411.     return anErr;
  412.     
  413. } // MFEGetItemIcon
  414.  
  415. // *****************************************************************************
  416.  
  417. PASCAL OSErr MFEGetEveryItemOnDesktop( const AEIdleUPP idleProcUPP,
  418.                                              AEDescList *objectList )
  419. {
  420.     OSErr        anErr = noErr;
  421.     
  422.     AppleEvent    theEvent = {typeNull, nil};
  423.     
  424.     objectList->descriptorType = typeNull;
  425.     objectList->dataHandle = nil;
  426.     
  427.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  428.                                               kAECoreSuite, kAEGetData, &theEvent );
  429.     if ( anErr == noErr )
  430.     {
  431.         
  432.         AEDesc        containerObj = { typeNull, nil };        // start with the null (application) container
  433.         AEDesc         propertyObject = {typeNull, nil};
  434.         
  435.         anErr = AEHMakePropertyObject( kDesktopFolderType, &containerObj, &propertyObject );
  436.         
  437.         if ( anErr == noErr )
  438.         {
  439.             DescType    selectAll = kAEAll;
  440.             AEDesc        allObjectsDesc = { typeNull, nil };
  441.             
  442.             anErr = AEHMakeSelectionObject( selectAll, &propertyObject, &allObjectsDesc );
  443.             if ( anErr == noErr )
  444.             {
  445.                 anErr = AEPutParamDesc( &theEvent, keyDirectObject, &allObjectsDesc );
  446.                 AEDisposeDesc( &allObjectsDesc );
  447.                 if ( anErr == noErr )
  448.                 {
  449.                     AppleEvent    theReply = {typeNull, nil};
  450.                     AESendMode    sendMode;
  451.                     
  452.                     if ( idleProcUPP == nil )
  453.                         sendMode = kAENoReply;
  454.                     else
  455.                         sendMode = kAEWaitReply;
  456.  
  457.                     anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  458.                                     kNoTimeOut, idleProcUPP, nil );
  459.                     
  460.                     AEDisposeDesc( &theEvent );
  461.                     
  462.                     if ( anErr == noErr )
  463.                     {
  464.                         anErr =  AEHGetHandlerError( &theReply );
  465.                         
  466.                         if ( !anErr && theReply.descriptorType != typeNull )
  467.                         {
  468.                             anErr = AEGetParamDesc( &theReply, keyDirectObject, typeAEList, objectList );
  469.                         }
  470.                         AEDisposeDesc( &theReply );
  471.                     }
  472.                 }
  473.             }
  474.         }
  475.     }
  476.     
  477.     return anErr;
  478.     
  479. } // MFEGetEveryItemOnDesktop
  480.  
  481. // *****************************************************************************
  482.  
  483. PASCAL OSErr MFEUpdateItemFSS( const FSSpecPtr fssPtr )
  484. {
  485.     OSErr            anErr = noErr;
  486.     AliasHandle        aliasHandle;
  487.     
  488.     anErr = NewAlias( nil, fssPtr, &aliasHandle);
  489.  
  490.     if ( anErr == noErr )
  491.     {
  492.         anErr = MFEUpdateItemAlias( aliasHandle );
  493.     }
  494.     
  495.     DisposeHandle( (Handle)aliasHandle );
  496.     
  497.     return anErr;
  498. } // MFEChangeFolderView
  499.  
  500. // *****************************************************************************
  501.  
  502. PASCAL OSErr MFEUpdateItemAlias( const AliasHandle aliasHandle )
  503. {
  504.     OSErr        anErr = noErr;
  505.     
  506.     AppleEvent    theEvent = {typeNull, nil};
  507.     
  508.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  509.                                               kAEFinderSuite, kAEUpdate, &theEvent );
  510.     if ( anErr == noErr )
  511.     {
  512.         AEDesc         aliasDesc = {typeNull, nil};
  513.         
  514.         anErr = AEHMakeAliasDesc( aliasHandle, &aliasDesc );
  515.         if ( anErr == noErr )
  516.         {
  517.             anErr = AEPutParamDesc( &theEvent, keyDirectObject, &aliasDesc );
  518.             AEDisposeDesc( &aliasDesc );
  519.             if ( anErr == noErr )
  520.             {
  521.                 AppleEvent    theReply = {typeNull, nil};
  522.                 
  523.                 anErr = AESend( &theEvent, &theReply, kAENoReply, kAENormalPriority,
  524.                                 kAEDefaultTimeout, nil, nil );
  525.                 AEDisposeDesc( &theEvent );
  526.                 
  527.                 if ( anErr == noErr )
  528.                 {
  529.                     anErr =  AEHGetHandlerError( &theReply );
  530.                     
  531.                     AEDisposeDesc( &theReply );
  532.                 }
  533.             }
  534.         }
  535.     }
  536.     
  537.     return anErr;
  538.     
  539. } // MFEChangeFolderView
  540.  
  541. // *****************************************************************************
  542.  
  543. PASCAL OSErr MFEOpenFile( const AEIdleUPP idleProcUPP,
  544.                                 FSSpec *fssPtr )
  545. {
  546.     OSErr        anErr = noErr;
  547.     
  548.     AppleEvent    theEvent = { typeNull, nil };
  549.     
  550.     anErr = AEHMakeAppleEventSignatureTarget( kFinderFileType, kFinderCreatorType,
  551.                                               kAECoreSuite, kAEOpen, &theEvent );
  552.     if ( anErr == noErr )
  553.     {
  554.         anErr = AEPutParamPtr( &theEvent, keyDirectObject, typeFSS, fssPtr, sizeof( FSSpec ) );
  555.         
  556.         if ( anErr == noErr )
  557.         {    
  558.             AppleEvent    theReply = {typeNull, nil};
  559.             AESendMode    sendMode;
  560.             
  561.             if ( idleProcUPP == nil )
  562.                 sendMode = kAENoReply;
  563.             else
  564.                 sendMode = kAEWaitReply;
  565.  
  566.             anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
  567.                             kNoTimeOut, idleProcUPP, nil );
  568.             
  569.             AEDisposeDesc( &theEvent );
  570.             
  571.             if ( anErr == noErr )
  572.             {        
  573.                 anErr =  AEHGetHandlerError( &theReply );
  574.                 AEDisposeDesc( &theReply );
  575.             }
  576.         }
  577.     }
  578.     
  579.     return anErr;
  580. }//end MFELaunchApplication
  581.  
  582.  
  583.